Dictionary Methods

The Dictionary object contains the following methods:

Clear

The Clear method clears all keys and values from the dictionary.

Syntax

Clear()

Example

The following example resets the dictionary and informs the user.

Copy
Clear
Sub clearDic()
    Dictionary.Clear()
    edtMessageBox.Text = "Dictionary has been cleared"
End Sub

Back to top

ExportToXml

The ExportToXml method dumps the data in the dictionary to an XML string.

Syntax

ExportToXml() As String

Remarks

Return Value: An XML string containing the contents of the dictionary.

Example

The following example gets an XML string from the dictionary and saves it to the local disk.

Copy
ExportToXml
Sub exportDic()
    Dim strXml
    strXml = Dictionary.ExportToXml
     
    'Save XML
    Set xmlDoc = CreateObject("Msxml2.DOMDocument")
    xmlDoc.loadXML(strXml)
    xmlDoc.save("C:\DictionaryData.xml")
End Sub

Back to top

FindValue

The FindValue method gets the value from a key.

Syntax

FindValue(Key As String)

Parameters

Parameter Required Description

Key

Yes

A valid dictionary key.

Remarks

Return Value: The value of the key.

Example

The following example gets the value for Key1 and displays it in a text box.

Copy
FindValue
Sub findValueDic()
    Dim Ret
    Ret = Dictionary.FindValue("Key1")
    edtMessageBox.Text = "The value for Key1 is " & Ret
End Sub

Back to top

GetKeyList

The GetKeyList method retrieves the list of existing keys to allow iteration of the contents of the dictionary.

Note: This method is not exposed in the CygNet Studio-wrapped global Dictionary object.

Syntax

GetKeyList() as Variant

Remarks

Return Value: An array of keys in the dictionary.

Example

The following example gets all the keys in the dictionary object and displays them in a list box.

Copy
GetKeyList
Sub getKeyList()
    Dim objDictionary
    Set objDictionary = CreateObject("CxScript.Dictionary")
     
    'Get key list
    Dim arrKeyList, item
    arrKeyList = objDictionary.GetKeyList()
     
    'Add keys to a list box
    For Each item in arrKeyList
        lstDictionary.AddString(item)
    Next
End Sub

Back to top

GetSize

The GetSize method gets the number of keys in the dictionary.

Syntax

GetSize() As Integer

Remarks

Return Value: An integer representing the number of keys in the dictionary.

Example

The following example displays the size of the dictionary in a text box.

Copy
GetSize
Sub getSizeDic()
    Dim intSize
    intSize = Dictionary.GetSize
    edtMessageBox.Text = "There are " & intSize & " keys in the dictionary"
End Sub

Back to top

InitializeFromXml

The InitializeFromXml method initializes the dictionary from an XML string.

Syntax

InitializeFromXml(Xml As String)

Parameters

Parameter Required Description

Xml

Yes

A valid dictionary XML string.

Remarks

The following is an example of XML that can be used.

Copy
Dictionary XML
<Dictionary>
    <Item Key="Key1">Value1</Item>
    <Item Key="Key2">Value2</Item>
    <Item Key="TestKey">Blah</Item>
</Dictionary>

Example

The following example initializes the dictionary from a sample XML string.

Copy
InitializeFromXml
Sub initXml()
    Dim strXml
    strXml = "<Dictionary><Item Key="Key1">Value1</Item</Dictionary>"
    Dictionary.InitializeFromXml strXml
    edtMessageBox.Text = "Dictionary initialized"
End Sub

Back to top

KeyExists

The KeyExists method checks if a key exists.

Syntax

KeyExists(Key As String) As Boolean

Parameters

Parameter Required Description

Key

Yes

The name of a key to check.

Remarks

Return Value: A Boolean indicating whether the key exists. If True, the key exists; if False, the key doesn’t exist.

Example

The following example informs the user whether or not TestKey exists.

Copy
KeyExists
Sub checkKey()
    Dim bRet
    bRet = Dictionary.KeyExists("TestKey")
     
    If bRet Then
        edtMessageBox.Text = "The key exists"
    Else
        edtMessageBox.Text = "The key does not exist"
    End If
End Sub

Back to top

Remove

The Remove method removes an element from the dictionary.

Syntax

Remove(Key As String)

Parameters

Parameter Required Description

Key

Yes

A valid key in the dictionary to remove.

Example

The following example removes a key from the dictionary.

Copy
Remove
Sub delKey()
    Dictionary.Remove "Key1"
    edtMessageBox.Test = "The key has been removed"
End Sub

Back to top

SetKeyValue

The SetKeyValue method sets a key-value pair.

Syntax

SetKeyValue(Key As String, Value As Variant)

Parameters

Parameter Required Description

Key

Yes

A valid key in the dictionary.

Value

Yes

A new value for the key. This will replace the old value.

Example

The following example updates a key with a new value.

Copy
SetKeyValue
Sub setValue()
    Dictionary.SetKeyValue "TestKey", "NewValue"
    edtMessageBox.Text = "TestKey has been updated"
End Sub

Back to top